package com.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.IUserConfigService;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("config/user")
public class UserConfigController {

	@Autowired
	private IUserConfigService userConfigService;
	
	
	@RequestMapping("")
	public ModelAndView configPage(){
		ModelAndView mv = new ModelAndView("userConfig/configList");
		mv.addObject("projectName",ProjectInfor.PLATFORM_NAME);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	
	@RequestMapping("list")
	public void list(HttpServletRequest request,
			HttpServletResponse response){
		List<Map<String, Object>> list = userConfigService.getList();
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
	
	@RequestMapping("fields/{type}")
	public void fields(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer type){
		List<Map<String, Object>> list = userConfigService.getFields(type);
		OutputUtil.returnString(response, JSONSerielizer.toJson(list, null, null));
	}
	
	@RequestMapping("add")
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response) {
		String userLevel = SessionUtil.getStringAttri(request, SessionData.levelKey);
		ModelAndView mv = null;
		if (!"9".equals(userLevel)) {
			mv = new ModelAndView("common/empty");
			mv.addObject("pageInfo", "对不起，您没有此权限！");
			return mv;
		}
		mv = new ModelAndView("userConfig/configForm");
		mv.addObject("titleName", "新增员工属性配置");
		mv.addObject("action", "new");
		return mv;
	}
	
	@RequestMapping("update/{id}")
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response, @PathVariable String id) {
		String userLevel = SessionUtil.getStringAttri(request, SessionData.levelKey);
		ModelAndView mv = null;
		if (!"9".equals(userLevel)) {
			mv = new ModelAndView("common/empty");
			mv.addObject("pageInfo", "对不起，您没有此权限！");
			return mv;
		}
		mv = new ModelAndView("userConfig/configForm");
		mv.addObject("titleName", "修改员工属性配置");
		mv.addObject("action", "edit");
		mv.addObject("id", id);
		return mv;
	}
	
	@RequestMapping("form/{id}")
	public void form(HttpServletRequest request,
			HttpServletResponse response, @PathVariable String id) {
		Map<String, Object> form = userConfigService.getForm(id);
		OutputUtil.returnString(response, JSONSerielizer.toJson(form, null, null));
	}
	
	@RequestMapping("save/{action}")
	public void save(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String action){
		String formData = request.getParameter("formData");
		Map<String, Object> inputMap = JSONSerielizer.toMap(formData);
		
		Map<String, Object> map = new HashMap<String, Object>();
		Boolean isSuccess = true;
		String info = null;
		Object fieldObj = inputMap.get("FieldName");
		Object contentObj = inputMap.get("Content");
		Object idObj = inputMap.get("Id");
		if (fieldObj==null || contentObj==null|| idObj==null) {
			isSuccess = false;
			info = "保存的数据有误!";
		} else {
			if ("new".equals(action)) {
				isSuccess = userConfigService.add(fieldObj.toString(), contentObj.toString());
				info = isSuccess ? "添加成功。" : "添加失败！";
			}else {//不能修改，只能生成新规则
				isSuccess = userConfigService.update(idObj.toString(), fieldObj.toString(), contentObj.toString());
				info = isSuccess ? "修改成功。" : "修改失败！";
			}
		}
		map.put("isSuccess", isSuccess);
		map.put("info", info);
		OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
	}
	
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response){
		String ids = request.getParameter("ids");
		Boolean isSuccess = userConfigService.delete(ids);
		OutputUtil.returnSuccess(response, isSuccess);
	}
	
}
